// 商城页面数据接口
const express = require("express");
const route = express.Router();
const pool = require("../utils/mysql");
// 引入验证
const Joi = require('joi')
// 获取商城大分类
route.get('/shop/class', (req, res) => {
    pool.query('SELECT * FROM pet_shopping_class', (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok', data: r })
        }
    })
})
// 获取所有小分类
route.get('/shop/small_all', (req, res) => {
    pool.query('SELECT * FROM pet_shopping_small', (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok', data: r })
        }
    })
})
// 通过大分类id获取商城小分类
route.get('/shop/small', (req, res) => {
    let { sp_class_id } = req.query
    let schema = Joi.object({
        sp_class_id: Joi.string().required()
    })
    let { error } = schema.validate(req.query)
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    pool.query('SELECT * FROM pet_shopping_small where sp_class_id = ?', [sp_class_id], (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok', data: r })
        }
    })
})
// 添加商城大分类
route.post('/shop/class_add', (req, res) => {
    let { sp_class_name } = req.body

    let schema = Joi.object({
        sp_class_name: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }


    let sql = `INSERT INTO pet_shopping_class (sp_class_id,sp_class_name) VALUES (NULL,?)`
    pool.query(sql, [sp_class_name], (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 删除商城大分类
route.delete('/shop/class_del', (req, res) => {
    let { sp_class_id } = req.query

    let schema = Joi.object({
        sp_class_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = `DELETE FROM pet_shopping_class WHERE sp_class_id=?`
    pool.query(sql, [sp_class_id], (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok' })
        }
    })
})



// 添加商城小分类
route.post('/shop/small_add', (req, res) => {
    let { sm_name, sp_class_id } = req.body

    let schema = Joi.object({
        sm_name: Joi.string().required(), // 必填
        sp_class_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = `INSERT INTO pet_shopping_small(sm_id, sm_name,sp_class_id) VALUES(NULL, ?,?)`
    pool.query(sql, [sm_name, sp_class_id], (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
// 删除商城小分类
route.delete('/shop/small_del', (req, res) => {
    let { sm_id } = req.query

    let schema = Joi.object({
        sm_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = `DELETE FROM pet_shopping_small WHERE sm_id=?`
    pool.query(sql, [sm_id], (err, r) => {
        // 错误返回原因
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 成功返回
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 查询所有商品  查询所有数据
route.get('/shop/all', (req, res) => {
    let { pno, pagesize } = req.query

    let schema = Joi.object({
        pno: Joi.string().required(), // 必填
        pagesize: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    // 从哪个数据开始查
    let startIndex = (pno - 1) * pagesize
    let sql = `SELECT * FROM pet_shopping LIMIT ?,?;SELECT count(*) as total FROM pet_shopping;`
    pool.query(sql, [parseInt(startIndex), parseInt(pagesize)], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 整理数据
            let [pets, total] = r
            total = total[0].total
            res.send({ code: 200, data: { pets, total, pno, pagesize } })
        }
    })

})

// 通过id获取商品
route.get('/shop/byid', (req, res) => {
    let { sp_id } = req.query

    let schema = Joi.object({
        sp_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    // 从哪个数据开始查
    let sql = `SELECT * FROM pet_shopping where sp_id = ?`
    pool.query(sql, [sp_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 整理数据
            res.send({ code: 200, data: r })
        }
    })
})

// 模糊查询商城
route.get('/shop/bykeyword', (req, res) => {
    let { keyword } = req.query

    let schema = Joi.object({
        keyword: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    let sql = `SELECT * FROM pet_shopping where sp_rmd like ?`
    pool.query(sql, [`%${keyword}%`], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 整理数据
            res.send({ code: 200, data: r })
        }
    })

})


// 通过大小分类获取商品
route.get('/shop/by_class_small', (req, res) => {
    let { sm_id } = req.query
    let schema = Joi.object({
        sm_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    pool.query('select * from pet_shopping where sm_id=?', [sm_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            // 整理数据
            res.send({ code: 200, data: r })
        }
    })


})

// 添加商品
route.post('/shop/add_goods', (req, res) => {
    let sql = `INSERT INTO pet_shopping VALUES ( NULL,?,?,NULL,?,?,?,?)`
    let { sp_name, sp_amount, img_price, sp_rmd, sp_class_id, sm_id } = req.body

    let schema = Joi.object({
        sp_name: Joi.string().required(), // 必填
        sp_amount: Joi.string().required(), // 必填
        img_price: Joi.string().required(), // 必填
        sp_rmd: Joi.string().required(), // 必填
        sp_class_id: Joi.string().required(), // 必填
        sm_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }


    pool.query(sql, [sp_name, sp_amount, img_price, sp_rmd, sp_class_id, sm_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
// 删除商品
route.delete('/shop/del_goods', (req, res) => {
    let { sp_id } = req.query

    let schema = Joi.object({
        sp_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = 'DELETE FROM pet_shopping WHERE sp_id=?'
    pool.query(sql, [sp_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 修改大类类别
route.put('/shop/update_class', (req, res) => {
    let { sp_class_name, sp_class_id } = req.body

    let schema = Joi.object({
        sp_class_name: Joi.string().required(), // 必填
        sp_class_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }


    let sql = "UPDATE pet_shopping_class SET sp_class_name=? WHERE sp_class_id=?"

    pool.query(sql, [sp_class_name, sp_class_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
// 修改小类分类
route.put('/shop/update_small', (req, res) => {
    let { sm_name, sm_id } = req.body

    let schema = Joi.object({
        sm_name: Joi.string().required(), // 必填
        sm_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "UPDATE pet_shopping_small SET sm_name=? WHERE sm_id=?"
    pool.query(sql, [sm_name, sm_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 修改商品信息
route.put('/shop/update_goods', (req, res) => {
    let { sp_name, sp_amount, img_price, sp_rmd, sp_id } = req.body

    let schema = Joi.object({
        sm_name: Joi.string().required(), // 必填
        sp_amount: Joi.string().required(), // 必填
        img_price: Joi.string().required(), // 必填
        sp_rmd: Joi.string().required(), // 必填
        sp_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "UPDATE pet_shopping SET sp_name=?,sp_amount=?,img_price=?, sp_rmd=? WHERE sp_id=?"
    pool.query(sql, [sp_name, sp_amount, img_price, sp_rmd, sp_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
module.exports = route